const path = require('path')
const webpack = require('webpack')
const CompressionPlugin = require('compression-webpack-plugin')
const system = require('./src/config/system')
const modifyVars = require('./src/config/theme')

function resolve(dir) {
  return path.join(__dirname, dir)
}
const isDev = process.env.NODE_ENV === 'development'
const isProd = process.env.NODE_ENV === 'production' // 查看是否正式服
const proApi = 'https://dev.iiot.hxct.com/' // 正式服api
module.exports = {
  productionSourceMap: false,
  publicPath: '/' + (isDev ? '' : 'mes-web'),
  configureWebpack: {
    plugins: [
      new webpack.DllReferencePlugin({
        context: process.cwd(),
        manifest: require('./public/vendor/vendor-manifest.json')
      })
    ]
  },
  chainWebpack(config) {
    config.resolve.alias
      .set('@', resolve('src'))
      .set('@c', resolve('src/components'))
      .set('@u', resolve('src/utils'))
      .set('@api', resolve('src/api'))
      .set('@assets', resolve('src/assets'))
      .set('@views', resolve('src/views'))
      .set('@layout', resolve('src/layout'))
    config.plugin('html').tap(args => {
      args[0].title = system.name
      args[0].baseUrl = '/' + (isDev ? '' : 'mes-web/')
      args[0].isDev = isDev
      return args
    })
    config.plugins.delete('prefetch')
    // ie兼容
    config.entry('main').add('babel-polyfill')
    // 处理svg图片
    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/svg'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/svg'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    if (!isDev) {
      // 开启gzip
      config.plugin('compressionPlugin')
        .use(new CompressionPlugin({
          test: /\.js$|\.html$|.\css/, // 匹配文件名
          threshold: 10240, // 对超过10k的数据压缩
          deleteOriginalAssets: false // 不删除源文件
        }))
        .end()
    }
  },
  css: {
    loaderOptions: {
      less: {
        modifyVars,
        // DO NOT REMOVE THIS LINE
        javascriptEnabled: true
      }
    }
  },
  devServer: {
    proxy: {
      '/devtest/basic-business': {
        //target: 'http://localhost:8030/',
        target: proApi,
        ws: false,
        changeOrigin: true,
        pathRewrite: { '^/devtest/basic-business': '/api/basic-business' }
      },
      // 本地开发使用
      '/devtest': {
        // target: 'http://10.8.4.63:8030/', // 戴俊忱
        // target: 'http://10.8.4.107:8030/', // 戴俊忱
        // target: 'http://test123.utools.club/', // 戴俊忱         
        // target: 'http://10.8.4.191:8030/', // 昌哥         
        target: proApi, // 测试环境线上
        ws: false,
        changeOrigin: true,
        pathRewrite: {
          '^/devtest': '/api'
        }
      },
      '/api': {
        target: proApi,
        changeOrigin: true,
        pathRewrite: { '^/api': '/api' }
      },
      '/junchen': {
        target: 'http://10.8.4.5:8030/',
        changeOrigin: true,
        pathRewrite: {
          '^/junchen': ''
        }
      }
    }
  },
  // css: { loaderOptions: { less: { javascriptEnabled: true } } },
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'less',
      patterns: [
        path.resolve(__dirname, 'src/assets/less/theme.less'),
      ]
    }
  }
}